/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.RegularExpression;
import com.mxg.blog.entity.User;
import com.mxg.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/29 14:53
 * 获取管理页数据
 */
@Controller
public class GetHomeData {

    private final UserService userService;

    public GetHomeData(UserService userService){
        this.userService = userService;
    }

    public ResponseEntity<Map<String, Object>> getHomeData(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        String userAccount = requestMap.get("usrAccount");

        if(requestMap.isEmpty())
        {
            resultMap.put("code", 1000);
            resultMap.put("message", "请登录后使用");
            return ResponseEntity.ok(resultMap);
        }else if(userAccount == null)
        {
            resultMap.put("code", 1000);
            resultMap.put("message", "请登录后再使用");
            return ResponseEntity.ok(resultMap);
        }

        User user;

        //正则判断
        RegularExpression regularExpression = new RegularExpression();
        //传入的是电话号码
        if (regularExpression.isPhoneNum(userAccount)) {
            //通过电话查询用户信息
            user = userService.getUserByUserPhoneNum(userAccount);
        }
        //传入的是邮箱
        else if (regularExpression.isEmail(userAccount)) {
            //通过邮箱查询用户值
            user = userService.getUserByUserEmail(userAccount);
        } else {
            resultMap.put("code", 3001);
            resultMap.put("message", "用户不存在");
            return ResponseEntity.ok(resultMap);
        }

        return ResponseEntity.ok(resultMap);
    }

}
